﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MMS.DAO;

namespace MMS
{
    public partial class UserControlMaterialList : UserControl
    {
        MaterialDAO materialDAO = new MaterialDAO(Param.ConnectionString.Value);

        public UserControlMaterialList()
        {
            InitializeComponent();
        }

        private void UserControlMaterialList_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            gridViewMaterial.OptionsBehavior.AutoPopulateColumns = false;
            PopulateData();
        }

        private void PopulateData()
        {
            
            List<MaterialDTO> materialList = new List<MaterialDTO>();
            materialList = materialDAO.SelectAll();

            gridControlMaterial.DataSource = materialList;
          
            Id.FieldName = "Id";
            MaterialName.FieldName = "Name";
            UnitName.FieldName = "UnitName";
            Desciption.FieldName = "Description";
            PriceByDay.FieldName = "PriceByDay";
            gridViewMaterial.BestFitColumns();
        }

        private MaterialDTO getSelectedMaterial()
        {
            MaterialDTO selectedMaterial= null;
            string materialId = gridViewMaterial.GetFocusedRowCellValue(Id).ToString();
            selectedMaterial = materialDAO.Select(materialId);
            return selectedMaterial;
        }

        private void barButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string itemName = e.Item.Name;
            if (itemName.Equals(bbiAdd.Name))
            {
                using (FormMaterialInfo f = new FormMaterialInfo())
                {
                    f.Text = MessageBundle.ADD_NEW_MATERIAL.Value;
                    f.ShowDialog();
                    PopulateData();
                }
            }
            else if (itemName.Equals(bbiEdit.Name))
            {
                MaterialDTO selectedMaterial = getSelectedMaterial();
                using (FormMaterialInfo f = new FormMaterialInfo(selectedMaterial))
                {
                    f.Text = MessageBundle.EDIT_MATERIAL.Value;
                    f.ShowDialog();
                    PopulateData();
                }

            }
            else if (itemName.Equals(bbiDelete.Name))
            {
                MaterialDTO selectedMaterial = getSelectedMaterial();
                MaterialDAO materialDAO = new MaterialDAO(Param.ConnectionString.Value);
                try
                {
                    DialogResult deleteResult = Utils.ShowAuthentication(MessageBundle.DELETE_WARNING_CONTENT.Value);
                    if (deleteResult == DialogResult.Yes)
                    {
                        materialDAO.Delete(selectedMaterial.Id);
                        LogUtil.log.Error("You have just deleted this Material" + selectedMaterial.Id);
                    }

                }

                catch (System.Exception ex)
                {
                    Utils.ShowError(MessageBundle.CANNOT_DELETE_CONTENT.Value);
                    LogUtil.log.Error("can not delete this Material" + selectedMaterial.Id + "since it is using  in OrderFrom_Material or Repository_Material");
                    LogUtil.log.Error(ex);
                }

                PopulateData();
            }

            else if (itemName.Equals(bbiExit.Name))
            {

            }
        }

    }
}
